#include<iostream>
extern "C"{
#include<linux/input.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include"bmp.h"
}


int win=0;//判断是否获得胜利
int chess[480][800];//标记黑棋和白棋的坐标

#ifdef BLACK  //黑棋胜
int date[225] = { 0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,
                  0,1,1,1,1,  1,0,0,0,0,  0,0,0,0,0,
                  0,2,2,2,2                         };
#elif WHITE   //白棋胜
int date[225] = { 0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,
                  0,2,2,2,2,  2,0,0,0,0,  0,0,0,0,0,
                  0,1,1,1,1                        };
#elif HE      //未分胜负
int date[225] = { 0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,
                  0,2,2,2,2,  0,0,0,0,0,  0,0,0,0,0,
                  0,1,1,1,1                        };
#endif

int *p1 = date; //指向date指针
// int draw_judg(int *p1);

//画棋盘 15*15
void draw_board(int color)
{
    //给屏幕画底色
    //draw_color(0xffffff);
   show_bmp(0,0,"./pic/back.bmp");
    //棋盘画成橙色
    for(int i=30;i<450;i++)
    {
        for(int j=60;j<510;j++)
        {
            LCD_draw_print(j,i,color);
        }
    }
    //画横线
    for(int i=30;i<=450;i++)
    {
        if(i%30==0)
        {
            for(int j=60;j<=510;j++)
            {
                LCD_draw_print(j,i,0x0);
            }
        }
    }
    //画纵线
    for(int j=60;j<=510;j++)
    {
        if(j%30==0)
        {
            for(int i=30;i<=450;i++)
            {
                LCD_draw_print(j,i,0x0);
            }
        }
        
    }
}

//画棋子,画黑棋
void black_chess(int x,int y)
{
    //以(x,y)为圆心画半径为10的圆
    for(int i=30;i<=480;i++)
    {
        for(int j=60;j<=550;j++)
        {
            if((i-y)*(i-y)+(j-x)*(j-x)<=10*10)
            {
                LCD_draw_print(j,i,0x0);
            }
        }
    }
}

//画棋子,画白棋
void white_chess(int x,int y)
{
    //以(x,y)为圆心画半径为10的圆
    for(int i=30;i<=480;i++)
    {
        for(int j=60;j<=550;j++)
        {
            if((i-y)*(i-y)+(j-x)*(j-x)<=10*10)
            {
                LCD_draw_print(j,i,0xfcfcfc);
            }
        }
    }
}

//五子棋棋盘位置初始化
void local_init()
{	
	for(int i=30;i<480;i+=30)
	{
		for(int j=60;j<800;j+=30)
		{
			chess[i][j]=0;//默认为0
		}
	}
}

//重新画棋子
void draw_chess()
{
    for(int i=30;i<480;i+=30)
    {
        for(int j=60;j<800;j+=30)
        {
            if(chess[i][j]==1)//画黑棋
            black_chess(j,i);
            else if(chess[i][j]==2)//画白棋
            white_chess(j,i);
            else
            continue;
        }
    }
}

//下棋函数
int play_chess(int* x1,int* y1,int* chess_color,int *x,int *y)
{
    int flag=0,x2=0,y2=0;     
    x2=(*x1-60)/30;
    y2=(*y1-30)/30;
    if((*x1-60)%30>15)//列数
    x2+=1;
    if((*y1-30)%30>15)//行数
    y2+=1;
    //将触摸屏坐标映射到(800*480)坐标系上
    *x1=x2*30+60;
    *y1=y2*30+30;
    if(*chess_color==1&&chess[*y1][*x1]==0)//黑棋
    {
        black_chess(*x1,*y1);
        *chess_color=0;
        cout<<"black:("<<*x1<<*y1<<")"<<endl;
        //printf("black:(%d,%d)\n",*x1,*y1);
        chess[*y1][*x1]=1;//记录黑棋坐标
        judg();//判断胜负
        flag=1;
    }
    else if(*chess_color==0&&chess[*y1][*x1]==0)//白棋
    {      
        white_chess(*x1,*y1);
        *chess_color=1;
         cout<<"white:("<<*x1<<*y1<<")"<<endl;
        //printf("white:(%d,%d)\n",*x1,*y1);
        chess[*y1][*x1]=2;//记录白棋坐标
        judg();//判断胜负
        flag=2;     
    }
    *x=*x1,*y=*y1;    
    return flag;
}

//判断胜负
int judg()//(x,y)
{
    int flag = 0;
    //判断横行和竖行
    for(int i=60;i<800;i+=30)
    {
        for(int j=30;j<480;j+=30)
        {
             //黑棋
            if(j<360&&chess[j][i]==1&&chess[j+30][i]==1&&chess[j+60][i]==1&&chess[j+90][i]==1&&chess[j+120][i]==1)
            {
                win++; flag=1; break;
            }
            if(i<680&&chess[j][i]==1&&chess[j][i+30]==1&&chess[j][i+60]==1&&chess[j][i+90]==1&&chess[j][i+120]==1)
            {
                win++; flag=1; break;
            }
            //白棋
            if(j<360&&chess[j][i]==2&&chess[j+30][i]==2&&chess[j+60][i]==2&&chess[j+90][i]==2&&chess[j+120][i]==2)
            {
                win++; flag=2; break;
            }
            if(i<680&&chess[j][i]==2&&chess[j][i+30]==2&&chess[j][i+60]==2&&chess[j][i+90]==2&&chess[j][i+120]==2)
            {
                win++; flag=2; break;
            }
        }
    }
    //判断左上到右下
    for(int i=60;i<800;i+=30)
    {
        for(int j=30;j<480;j+=30)
        {
            //黑棋
            if(i<680&&j<360&&chess[j][i]==1&& chess[j+30][i+30]==1&&chess[j+60][i+60]==1&&chess[j+90][i+90]==1&&chess[j+120][i+120]==1)
            {
                win++; flag=1; break;
            }
            //白棋
            if(i<680&&j<360&&chess[j][i]==2&&chess[j+30][i+30]==2&&chess[j+60][i+60]==2&&chess[j+90][i+90]==2&&chess[j+120][i+120]==2)
            {
                win++; flag=2; break;
            }
        }
    }
    //判断右上到左下
    for(int i=60;i<800;i+=30)
    {
        for(int j=30;j<480;j+=30)
        {
            //黑棋
            if(i<680&&j>120&&chess[j][i]==1&& chess[j-30][i+30]==1&&chess[j-60][i+60]==1&&chess[j-90][i+90]==1&&chess[j-120][i+120]==1)
            {
                win++; flag=1; break;
            }
            //白棋
            if(i<680&&j>120&&chess[j][i]==2&&chess[j-30][i+30]==2&&chess[j-60][i+60]==2&&chess[j-90][i+90]==2&&chess[j-120][i+120]==2)
            {
                win++; flag=2; break;
            }
        }
    }
   return flag; 
}


//开始游戏
void play_game()
{
    int x1=-1,y1=-1;
    int x2=-1,y2=-1;
    int x=-1,y=-1;//保存上一次下棋的位置
    int chess_color=1;//1:黑棋 0:白棋
    //1.打开文件
    int fd=open("/dev/input/event0",O_RDONLY);
    //if(fd == -1){
    //perror("open_event0:");}
    //2.解析文件
        //2.1定义结构体变量，接收信息
        struct input_event ev;
        //2.2读文件信息
        draw_board(qipan_color[p]); 
       while(1)
       {
            int ret=read(fd,&ev,sizeof(ev));
            //如果读的内容不完整，就跳过
            if(ret!=sizeof(ev))
            {
                continue;
            }
            int flag =0;

            flag = draw_judg(p1);  
        }      
    //关闭文件
    close(fd);
}

//主函数
void fivechess_game()
{
     //2.开始界面
    show_bmp(0,0,"./pic/kuang.bmp");
    int x_start=60,y_start=30;
    int x_end=510,y_end=450;
    int flag=game_func(x_start,y_start,x_end,y_end);//点击开始界面按钮，判断是否实现该功能
    if(flag)//实现
    {  //3.开始游戏
         //4. 画棋盘及游戏内界面
        draw_board(qipan_color[p]); 
        //5.五子棋棋盘位置初始化
        local_init();
        //6.开始下棋
        play_game();
        //6.1轮流下棋
        //6.1.1获取下棋坐标
        //6.1.2判断是黑棋还是白棋，下棋
        //6.2判断胜负
    }
}

//根据指针画棋和判断胜负
int draw_judg(int *p1) 
{   
    int xx, yy;
    int flag =0;
    for(int n=0; n<225; ++n)
    {
        if(p1[n] == 1){ 
            xx = (n/15)*30+30;
            yy = (n%15)*30+60;     
            chess[xx][yy] = 1;
            cout<<"p1["<< n <<"]="<< date[n] <<"  "<<"("<<xx<<","<<yy<<")"<<endl;
            //printf("p1[%d]= %d  (%d,%d)\n", n, date[n], xx, yy);
        }
     
        else if(p1[n] == 2){
            xx = (n/15)*30+30;
            yy = (n%15)*30+60;  
            chess[xx][yy] = 2;
            cout<<"p1["<< n <<"]="<< date[n] <<"  "<<"("<<xx<<","<<yy<<")"<<endl;
            //printf("p1[%d]= %d  (%d,%d)\n", n, date[n], xx, yy);           
        }      
    }
    draw_chess();
    flag = judg();

    
    //printf("win=%d  flag= %d\n", win, flag);
    if(win)//判断胜负
    {
        win=0;
        if(flag==1)
        {
            //黑方胜
            show_bmp(135,170,"./pic/blackwin.bmp");
            cout<<"black:win!!!"<<endl;
            //printf("black:win!!!\n");
            game_func(60,30,510,450);
            //重新回到首页面
            fivechess_game();
        }                
        else if(flag==2)
        {
            //白方胜
            show_bmp(135,170,"./pic/whitewin.bmp");
            cout<<"white:win!!!"<<endl;
            //printf("white:win!!!\n"); 
            game_func(60,30,510,450);
            //重新回到首页面
            fivechess_game();
        }
    }
}

